package com.polaris.live.ui.main.activity

import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager2.widget.ViewPager2
import com.polaris.live.R
import com.polaris.live.common.R.color
import com.polaris.live.common.adapter.ViewPagerAdapter
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.util.ImmersionBarBuilder
import com.polaris.live.common.util.JsonUtils
import com.polaris.live.databinding.ActivityUserGameRankBinding
import com.polaris.live.resp.back_resp.PartyGameResp
import com.polaris.live.ui.main.adapter.PartyGameRankTypeAdapter
import com.polaris.live.ui.main.fragment.rank.PartyRankFragment
import com.polaris.live.utils.getIntOrString
import com.polaris.live.viewmodel.login.UserRankViewModel

/**
 * 用户榜单
 *
 * @author Created by lucas on 2023/10/16 17:10
 * @since 1.0.0
 */
class UserGameRankActivity : BaseVbActivity<UserRankViewModel, ActivityUserGameRankBinding>() {

    private val mInitPage by lazy { intent.getIntOrString("page", 0) }

    override fun ImmersionBarBuilder.buildImmersionBar() {
        setTitleBar(mBinding.barView)
        setStatusBarDarkFont(false)
        setNavigationBarColor(color.color_0f112a)
        setNavigationBarDarkFont(false)
    }

    private val mGameList: List<PartyGameResp>? by lazy {
        val mGame = intent.getStringExtra("gameResp")
        mGame?.let { JsonUtils.fromJson<List<PartyGameResp>>(it) }
    }

    private val mAdapter by lazy { PartyGameRankTypeAdapter() }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.gameTypeRecycler.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            visibility = View.VISIBLE
        }
        initData()
        mAdapter.setList(mGameList)
    }

    private fun initData() {
        val listFragments = mutableListOf<Fragment>()
        mGameList?.forEach { itemData ->
            listFragments.add(PartyRankFragment().apply {
                arguments = bundleOf(
                    "gameResp" to itemData
                )
                setOnChange {
                    if (it == 0) {
                        this@UserGameRankActivity.mBinding.root.setBackgroundResource(R.drawable.bg_bangdan)
                    } else {
                        this@UserGameRankActivity.mBinding.root.setBackgroundResource(R.drawable.bg_party_game_month_list)
                    }
                }
            })
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            mAdapter.setSelect(position)
            mBinding.vpList.setCurrentItem(position, true)
        }
        mBinding.vpList.adapter = ViewPagerAdapter(listFragments, this)

        mBinding.vpList.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                mAdapter.setSelect(position)
            }
        })

        if (mInitPage != 0 && mInitPage - 1 <= listFragments.size - 1) {
            mAdapter.setSelect(mInitPage - 1)
            mBinding.gameTypeRecycler.smoothScrollToPosition(mInitPage - 1)
            mBinding.vpList.setCurrentItem(mInitPage - 1, false)
        }
    }
}